package com.zhiche.lisa.bms.service.psup.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.mapper.psup.PsupVehicleContrastMapper;
import com.zhiche.lisa.bms.dao.model.psup.PsupVehicleContrast;
import com.zhiche.lisa.bms.enums.SdCommonEnum;
import com.zhiche.lisa.bms.pojo.vo.psup.PsupVehicleContrastVO;
import com.zhiche.lisa.bms.service.psup.PsupVehicleContrastService;
import com.zhiche.lisa.core.supports.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 人送供方车型对照表 服务实现类
 * </p>
 *
 * @author linbao
 * @since 2019-06-18
 */
@Service
@Slf4j
public class PsupVehicleContrastServiceImpl extends ServiceImpl<PsupVehicleContrastMapper, PsupVehicleContrast> implements PsupVehicleContrastService {

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public Page<PsupVehicleContrastVO> querySdVehicleContrastPage(Page<PsupVehicleContrast> page) {
        EntityWrapper<PsupVehicleContrast> ew = this.buildCondition(page.getCondition());
        List<PsupVehicleContrast> vehicleContrastList = baseMapper.selectPage(page, ew);

        Page<PsupVehicleContrastVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        if (CollectionUtils.isNotEmpty(vehicleContrastList)) {
            List<PsupVehicleContrastVO> voList = Lists.newArrayList();
            vehicleContrastList.forEach(vehicleContrast -> {
                PsupVehicleContrastVO vo = new PsupVehicleContrastVO();
                BeanUtils.copyProperties(vehicleContrast, vo);
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    /**
     * 批量新增
     *
     * @param psupVehicleContrastVOList
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertBatchSdVehicleContrast(List<PsupVehicleContrastVO> psupVehicleContrastVOList) {
        if (CollectionUtils.isNotEmpty(psupVehicleContrastVOList)) {

            EntityWrapper<PsupVehicleContrast> ew = new EntityWrapper<>();
            List<PsupVehicleContrast> insertVehicleContrastList = Lists.newArrayList();
            for (PsupVehicleContrastVO psupVehicleContrastVO : psupVehicleContrastVOList) {
                if (StringUtils.isBlank(psupVehicleContrastVO.getStVehicle()) || StringUtils.isBlank(psupVehicleContrastVO.getCnVehicle())) {
                    throw new BaseException("合同车型或标准车型为空, 请检查!");
                }
                if (StringUtils.isBlank(psupVehicleContrastVO.getOilTypeStr())) {
                    throw new BaseException("燃油类型为空");
                }
                ew.eq("st_vehicle", psupVehicleContrastVO.getStVehicle());
                int count = this.selectCount(ew);
                if (count > 0) {
                    throw new BaseException("标准车型已存在, 请检查!");
                }
                PsupVehicleContrast insertVehicleContast = new PsupVehicleContrast();
                BeanUtils.copyProperties(psupVehicleContrastVO, insertVehicleContast);
                insertVehicleContast.setCreateTime(new Date());
                if (SdCommonEnum.OIL_TYPE_0.getCode().equals(psupVehicleContrastVO.getOilTypeStr())) {
                    insertVehicleContast.setOilType(SdCommonEnum.OIL_TYPE_0.getCode());
                } else if (SdCommonEnum.OIL_TYPE_92.getCode().equals(psupVehicleContrastVO.getOilTypeStr())) {
                    insertVehicleContast.setOilType(SdCommonEnum.OIL_TYPE_92.getCode());
                }
                insertVehicleContrastList.add(insertVehicleContast);
            }
            this.insertBatch(insertVehicleContrastList);
        }
    }

    /**
     * 获取所有的车型信息
     *
     * @return
     */
    @Override
    public List<PsupVehicleContrast> getVehicleLit() {
        EntityWrapper<PsupVehicleContrast> ew = new EntityWrapper<>();
        ew.setSqlSelect("distinct cn_vehicle");
        List<Object> objectList = this.selectObjs(ew);
        if (CollectionUtils.isNotEmpty(objectList)) {
            List<PsupVehicleContrast> ctVehilceList = Lists.newArrayList();
            objectList.forEach(obj -> {
                PsupVehicleContrast vehicleContrast = new PsupVehicleContrast();
                vehicleContrast.setCnVehicle(obj.toString());
                ctVehilceList.add(vehicleContrast);
            });
            return ctVehilceList;

        }
        return null;
    }

    /**
     * 查询条件
     *
     * @param condition
     * @return
     */
    private EntityWrapper<PsupVehicleContrast> buildCondition(Map<String, Object> condition) {
        EntityWrapper<PsupVehicleContrast> ew = new EntityWrapper<>();
        ew.orderBy("id desc");
        if (Objects.nonNull(condition)) {
            Object stVehicle = condition.get("stVehicle");
            if (Objects.nonNull(stVehicle) && StringUtils.isNotBlank(stVehicle.toString())) {
                ew.like("st_vehicle", stVehicle.toString());
            }
            Object cnVehicle = condition.get("cnVehicle");
            if (Objects.nonNull(cnVehicle) && StringUtils.isNotBlank(cnVehicle.toString())) {
                ew.like("cn_vehicle", cnVehicle.toString());
            }
            Object oilType = condition.get("oilType");
            if (Objects.nonNull(oilType) && StringUtils.isNotBlank(oilType.toString())) {
                ew.eq("oil_type", oilType);
            }
        }
        return ew;
    }
}
